package org.paniit.welcometoportland;




import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Ethnicity extends CustomWindow {
	private int stage = 0;
	private int countryCode = 0; 
	private String languageCode = " ";
	private Intent intent;
	private Spinner countriesSpinner;
    private Spinner languagesSpinner;
    private CheckBox checkBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    public static final String INTENT_STAGE = "stage";
    public static final String INTENT_COUNTRYCODE = "countryCode";
    public static final String INTENT_LANGUAGECODE = "languageCode";
	public static final String PREF_NAME = "W2PdxPrefsFile";
	public static final String PREF_CHECKBOX1 = "PREF_CHECKBOX1";
	public static final String PREF_CHECKBOX2 = "PREF_CHECKBOX2";
	public static final String PREF_CHECKBOX3 = "PREF_CHECKBOX3";
	public static final String PREF_COUNTRYSPINNER = "PREF_COUNTRYSPINNER";
	public static final String PREF_LANGUAGESPINNER = "PREF_LANGUAGESPINNER";
	
	SharedPreferences pref;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        //Inflate view
        setContentView(R.layout.ethnicity_view);
        
              
        //Get reference to UI widgets
        countriesSpinner = (Spinner)findViewById(R.id.countries_list);
        languagesSpinner = (Spinner)findViewById(R.id.languages_list);
        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        Button nextButton  =  (Button) findViewById(R.id.nextbutton);
       
        //Create the Array List for Countries and Languages
        //final String[] countries; 
        //final String[] languages;
        
        //Create Array Adapter to bind to the array to the ListView
        final ArrayAdapter<String> countriesAdapter = 
        	        new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, COUNTRIES);
        final ArrayAdapter languagesAdapter = 
	        		new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, LANGUAGES );
        
        //setting the layout resource to create dropdown views
        countriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        languagesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        //Bind the array adapter to list view
        countriesSpinner.setAdapter(countriesAdapter);
        languagesSpinner.setAdapter(languagesAdapter);
        
        //Setting onClickListener for widgets
        countriesSpinner.setOnItemSelectedListener(new CountriesOnItemSelectedListener());
        languagesSpinner.setOnItemSelectedListener(new LanguagesOnItemSelectedListener());
        
        checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        	public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked)
            {doCheckedMath(isChecked, R.id.checkbox1);}});
			
        checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked)
			{doCheckedMath(isChecked, R.id.checkbox2);}});
				
        checkBox3.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked)
			{doCheckedMath(isChecked, R.id.checkbox3);}});
        
        intent = new Intent();
        
        nextButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v)
			{
				savePreferences();
				intent.setClass(Ethnicity.this, Categories.class);
				intent.putExtra(INTENT_STAGE, stage);
				intent.putExtra(INTENT_COUNTRYCODE, countryCode);
				intent.putExtra(INTENT_LANGUAGECODE, languageCode);
				startActivity(intent);
			}});
        
      //Update UI from Preferences
        pref = getSharedPreferences(PREF_NAME, 0);
        updateUIFromPreferences();
	}//onCreate
	
	//Called at the start of the visible lifetime
	@Override
	public void onStart()
	{
		super.onStart();
		updateUIFromPreferences();
	}
	//Called at the end of active lifetime
	@Override
	public void onPause()
	{
		super.onPause();
		savePreferences();
	}
	
	private class CountriesOnItemSelectedListener implements OnItemSelectedListener 
    {
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
		{
			countryCode = COUNTRIESCODE[pos];
			//Toast.makeText(Ethnicity.this, "The selected country code is: " +
				       //String.valueOf(pos), Toast.LENGTH_SHORT).show();
		}
		public void onNothingSelected(AdapterView<?> arg0){}
    }
	
	private class LanguagesOnItemSelectedListener implements OnItemSelectedListener
	{
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
		{
			languageCode = LANGUAGESCODE[pos];
			//Toast.makeText(Ethnicity.this, "The selected country code is: " +
		       //languageCode, Toast.LENGTH_SHORT).show();
		}
		public void onNothingSelected(AdapterView<?> arg0){}
		
	}
    
	private void doCheckedMath(boolean isChecked, int resourceId)
	{
		int amt = 0;
		switch(resourceId)
		{
			case R.id.checkbox1:
				amt = 1; break;
			case R.id.checkbox2:
				amt = 10; break;
			case R.id.checkbox3:
				amt = 100; break;
		}
		
		if(isChecked)
			stage += amt;
		else
			if(stage > 0)
				stage -= amt;
		//Toast.makeText(getApplicationContext(), String.valueOf(stage), Toast.LENGTH_SHORT).show();
	}
	
	private void updateUIFromPreferences()
	{
		//Get settings
		int countryIndex = pref.getInt(PREF_COUNTRYSPINNER, 219);
		int languageIndex = pref.getInt(PREF_LANGUAGESPINNER, 2);
		boolean cb1Checked = pref.getBoolean(PREF_CHECKBOX1, false);
		boolean cb2Checked = pref.getBoolean(PREF_CHECKBOX2, false);
		boolean cb3Checked = pref.getBoolean(PREF_CHECKBOX3, false);
			
		//Update UI
		countriesSpinner.setSelection(countryIndex);
		languagesSpinner.setSelection(languageIndex);
		checkBox1.setChecked(cb1Checked);
		checkBox2.setChecked(cb2Checked);
		checkBox3.setChecked(cb3Checked);
	}
	
	private void savePreferences()
	{
		//Get UI settings
		int countryIndex = countriesSpinner.getSelectedItemPosition();
		int languageIndex = languagesSpinner.getSelectedItemPosition();
		boolean cb1Checked = checkBox1.isChecked();
		boolean cb2Checked = checkBox2.isChecked();
		boolean cb3Checked = checkBox3.isChecked();
		
		//Save settings
		Editor editor = pref.edit();
		editor.putInt(PREF_COUNTRYSPINNER, countryIndex);
		editor.putInt(PREF_LANGUAGESPINNER, languageIndex);
		editor.putBoolean(PREF_CHECKBOX1, cb1Checked);
		editor.putBoolean(PREF_CHECKBOX2, cb2Checked);
		editor.putBoolean(PREF_CHECKBOX3, cb3Checked);
		editor.commit();
		
	}
	static final String[] COUNTRIES = new String[]{
    	"Afghanistan", "Albania", "Algeria", "Andorra",    "Angola", "Anguilla", "Antarctica", 
    	"Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Ascension Island",
    	"Australia", "Austria", "Azerbaijan", "Bahamas","Bahrain", 
    	"Bangladesh", "Barbados", "Belarus", "Belgium","Belize",
    	"Benin", "Bermuda", "Bhutan", "Bolivia","Bosnia and Herzegovina",
    	"Botswana", "Brazil", "British Indian Ocean Territory", 
    	"British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
    	"Cambodia", "Cameroon", "Canada", "Cape Verde",
    	"Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
    	"Colombia", "Comoros","Congo Democratic Republic of the", "Congo, Republic of the", 
    	"Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus",
    	"Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador",
    	"Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", 
    	"Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland",
    	"France", "French Guiana", "French Polynesia",  "Gabon", "Gambia" ,"Gaza Strip ", "Georgia", 
    	"Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", 
    	"Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", 
    	"Guyana", "Haiti", "Honduras", 	"Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",
    	"Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan",
    	"Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", 
    	"Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", 
    	"Macau", "Macedonia, Republic of", "Madagascar","Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
       	"Marshall Islands",    "Martinique", "Mauritania", "Mauritius", "Mayotte", 
       	"Mexico", "Micronesia", "Moldova",    "Monaco", "Mongolia", "Montenegro", "Montserrat",
       	"Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", 
       	"Netherlands", "Netherlands Antilles", "Nepal", "New Caledonia", "New Zealand",
       	"Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Ireland",
       	"Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", 
       	"Papua New Guinea", "Paraguay", "Peru", "Philippines",
       	"Poland", "Portugal", "Qatar", "Reunion", "Romania", 
       	"Russia", "Rwanda", "Saint Barthelemy ", "Saint Helena", 
       	"Saint Kitts and Nevis", "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", 
       	"Saint Vincent and the Grenadines", "Samoa", "Sao Tome and Principe", "Saudi Arabia",
       	"Senegal", "Serbia ", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", 
       	"Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea",  
       	"Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", 
       	"Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tokelau",
       	"Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", 
       	"Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine",
       	"United Arab Emirates", "United Kingdom", "United States of America ", 
       	"Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "U.S. Virgin Islands", 
       	"Wallis and Futuna", "West Bank", "Yemen", "Zambia", "Zimbabwe"        			
    };
	
    static final int COUNTRIESCODE[] = new int[]{93, 355, 213, 376, 244, 1264, 6721, 1268, 54,
    	     374, 297, 247, 61, 43, 994, 1242, 973, 880, 1246, 375, 32, 501, 229, 1441, 975, 591,
    	    387, 267, 55, 246, 1284, 673, 359, 226, 257, 855, 237, 1, 238, 1345, 236, 235, 56, 86, 57, 
    	    269,243, 242, 682, 506, 225, 385, 53, 357, 420, 45, 253, 1767, 1809, 670, 593, 20, 503, 240, 291, 372,
    	    251, 500, 298, 679, 358, 33, 594, 689, 241, 220, 970, 995, 49, 233, 350, 30, 299, 1473, 590, 1671,
    	   	502, 224, 245, 592, 509, 504, 852, 36, 354, 91, 62, 964, 98, 353, 972, 39, 1876, 81, 962,
    	   	7, 254, 686, 965, 996, 856, 371, 961, 266, 231, 218, 423,370, 352, 853, 389, 261, 265,
    	   60, 960, 223, 356, 692, 596, 222, 230, 262, 52, 691, 373, 377, 976, 382, 1664, 212, 258, 95, 264, 674,  
    	   31, 599, 977, 687, 64, 505, 227, 234, 683, 6732, 850, 4428, 1670, 47, 968, 92, 680, 507, 675, 595,
    	   51, 63, 48, 351, 974, 262, 40, 7, 250, 590, 290, 1869, 1758, 590, 508, 1784, 685, 239, 966, 221, 381, 
    	   248, 232, 65, 421, 386, 677, 252, 27, 82, 34, 94, 249, 597, 268, 46,
    	   41, 963, 886, 992, 255, 66, 228, 690, 676, 1868, 216, 90, 993, 1649, 688, 256, 380, 971, 44, 1, 598,
    	   998, 678, 58, 84, 1340, 681, 970, 967, 260, 263};

    
    static final String[] LANGUAGES = new String[]{"Chinese", "Spanish", "English", "Hindi", "French", "Arabic",
    	          "Malay/Indonesian", "Portuguese", "Bengali", "Russian", "Japanese", "German Standard", "Punjabi",
    	          "Javanese", "Telugu", "Marathi", "Vietnamese", "Korean", "Tamil", "Italian" };
    
    static final String[] LANGUAGESCODE = new String[]{"chi", "spa", "eng", "hin", "fra","ara", "ind", "por", "ben", "rus",
    	        "jpn", "ger", "pan", "jav", "tel", "mar", "vie", "kor", "tam", "ita"};
}

  
